package com.stylefeng.guns.modular.stockmanagement.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.dto.GoodsOverflowAddRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.system.model.GoodsOverflow;
import com.stylefeng.guns.modular.stockmanagement.service.IGoodsOverflowService;

import java.util.Date;

/**
 * 商品报溢控制器
 *
 * @author LiuXin
 * @Date 2018-09-12 21:40:44
 */
@Controller
@RequestMapping("/goodsOverflow")
public class GoodsOverflowController extends BaseController {

    private String PREFIX = "/stockmanagement/goodsOverflow/";

    @Autowired
    private IGoodsOverflowService goodsOverflowService;

    /**
     * 跳转到商品报溢首页
     */
    @RequestMapping("")
    public String index(Model model) {
        String overflowOrderNumber = "BY-" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
        model.addAttribute("overflowOrderNumber", overflowOrderNumber);
        return PREFIX + "goodsOverflow.html";
    }

    /**
     * 跳转到添加商品报溢
     */
    @RequestMapping("/goodsOverflow_add")
    public String goodsOverflowAdd() {
        return PREFIX + "goodsOverflow_add.html";
    }

    /**
     * 跳转到修改商品报溢
     */
    @RequestMapping("/goodsOverflow_update/{goodsOverflowId}")
    public String goodsOverflowUpdate(@PathVariable Integer goodsOverflowId, Model model) {
        GoodsOverflow goodsOverflow = goodsOverflowService.selectById(goodsOverflowId);
        model.addAttribute("item",goodsOverflow);
        LogObjectHolder.me().set(goodsOverflow);
        return PREFIX + "goodsOverflow_edit.html";
    }

    /**
     * 获取商品报溢列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return goodsOverflowService.selectList(null);
    }

    /**
     * 新增商品报溢
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@RequestBody GoodsOverflowAddRequest request) {
        boolean success = goodsOverflowService.insertOverflowAndGoods(request);
        if(success){
            String overflowOrderNumber = "BY-" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
            return SUCCESS_TIP.setResultData(overflowOrderNumber);
        }
        return new ErrorTip(500,"删除失败,请稍后再试！");
    }

    /**
     * 删除商品报溢
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer goodsOverflowId) {
        goodsOverflowService.deleteById(goodsOverflowId);
        return SUCCESS_TIP;
    }

    /**
     * 修改商品报溢
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(GoodsOverflow goodsOverflow) {
        goodsOverflowService.updateById(goodsOverflow);
        return SUCCESS_TIP;
    }

    /**
     * 商品报溢详情
     */
    @RequestMapping(value = "/detail/{goodsOverflowId}")
    @ResponseBody
    public Object detail(@PathVariable("goodsOverflowId") Integer goodsOverflowId) {
        return goodsOverflowService.selectById(goodsOverflowId);
    }
}
